package com.itinsys.enma.web;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.Career;
import com.itinsys.enma.domain.response.CareerResponse;
import com.itinsys.enma.domain.response.EnmaSubjectResponse;
import com.itinsys.enma.domain.response.Response;
import com.itinsys.enma.manager.CareerManager;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.SchoolManager;
import com.itinsys.enma.service.SingletonService;

@Controller
@RequestMapping({ "/career" })
public class CareerFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");
	
	@Autowired
	private CareerManager careerManager;
	
	@Autowired
	private SchoolManager schoolManager;
	
	@Autowired
	private ResponseManager responseManager;
	
	@Autowired
	private SingletonService singletonService;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public CareerResponse getAllCareer() {
		return careerManager.getAllCareers();
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public CareerResponse addCareer(@RequestBody Career career,HttpServletResponse response) {
		Career newCareer = new Career();
		newCareer.setDescription(career.getDescription());
		newCareer.setSchool(schoolManager.getSchoolById(career.getSchool().getId()));		
		CareerResponse careerResponse = careerManager.addCareer(newCareer);
		if(careerResponse.getResponseCode()==Response.OK)
			singletonService.collectionsInit();
		return careerResponse;
	}

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public CareerResponse editCareer(@RequestBody Career career,HttpServletResponse response) {
		CareerResponse careerResponse = careerManager.updateCareer(career);
		if(careerResponse.getResponseCode()==Response.OK)
			singletonService.collectionsInit();
		return careerResponse;
	}
	

	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public CareerResponse deleteCareer(@RequestBody Map<String,String> careerDTO,HttpServletResponse response) {
		Career career = new Career();
		career.setId(Integer.parseInt(careerDTO.get("id")));
		CareerResponse careerResponse = careerManager.deleteCareer(career);
		if(careerResponse.getResponseCode()==Response.OK)
			singletonService.collectionsInit();
		return careerResponse;
	}

	@RequestMapping(value = { "/getSubjectsByCareer" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public EnmaSubjectResponse getAllSubjectsByCareer(@RequestBody Integer idCareer,	HttpServletResponse response ) {
		return careerManager.getAllSubjectsByCareer(idCareer);
	}
	
}
